﻿Imports Microsoft.VisualStudio.DebuggerVisualizers
Imports System.Collections.Generic
Imports System.Data.Objects
Imports System.Data.Objects.DataClasses
Imports System.Drawing
Imports System.Windows.Forms

<Assembly: DebuggerVisualizer(GetType(EntityCollectionVisualizer), GetType(EntityCollectionVisualizerObjectSource), _
 Target:=GetType(RelatedEnd), Description:="EntityCollection visualizer")> 

Public Class EntityCollectionVisualizer
    Inherits DialogDebuggerVisualizer


    Protected Overrides Sub Show(ByVal windowService As IDialogVisualizerService, ByVal objectProvider As IVisualizerObjectProvider)

        Dim ds As New DataSet()
        ds.ReadXml(objectProvider.GetData(), XmlReadMode.ReadSchema)

        Dim dtValues As DataTable = ds.Tables(Constants.TABLE_VALUES)

        Using form As New EntityCollectionVisualizerForm
            Dim errMsg As Object = ds.ExtendedProperties(Constants.EXT_PROP_ERROR_MESSAGE)
            If (errMsg Is Nothing) Then
                form.dgvData.DataSource = dtValues
                For i As Integer = 2 To form.dgvData.Columns.Count - 1 Step 2
                    ' hide columns with changeState
                    ' the grid will show the state on the datacolumn by formatting it
                    form.dgvData.Columns(i).Visible = False
                Next

                Utility.InitStatesGrid(form.dgvStates, ds)

                form.lblCount.Text = String.Format("{0} entities", _
                                                    dtValues.Rows.Count)
            Else
                form.lblCount.Text = errMsg.ToString()
                form.lblCount.ForeColor = Color.Red
            End If

            form.ShowDialog()
        End Using
    End Sub

End Class
